//
//  CHHHttpRequestService.m
//  RequestText
//
//  Created by imac on 15/9/8.
//  Copyright (c) 2015年 imac. All rights reserved.
//

#import "CHHHttpRequestService.h"
#import "SXLoadingView.h"
#define loadingTime 1.5
@implementation CHHHttpRequestService
+(void)GET:(NSString *)path success:(SuccessCallBack)success failure:(FailureCallBack)fail withView:(UIView *)vi
{
    [SXLoadingView showProgressHUD:@"加载中"];
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    NSLog(@"url---%@",path);
    [manager GET:path parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [SXLoadingView hideProgressHUD];
        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        
        BOOL succe=NO;
        if ([[dic valueForKey:@"code"]isKindOfClass:[NSNumber class]]) {
            if ([[dic valueForKey:@"code"]intValue]==2) {
                succe=YES;
            }
        }
        if ([[dic valueForKey:@"code"]isKindOfClass:[NSString class]]) {
            if ([[dic valueForKey:@"code"]isEqualToString:@"2"]) {
                succe=YES;
            }
        }
        if (succe) {
        }
        else
        {
            
        }
        success(responseObject,succe,dic);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [SXLoadingView hideProgressHUD];
        [SXLoadingView showAlertHUD:@"加载失败" duration:loadingTime];
        fail(error);
    }];
    
}
+ (void)POST:(NSString*)path withParameters:(NSDictionary*)params success:(SuccessCallBack)success failure:(FailureCallBack)fail withView:(UIView *)vi
{   [SXLoadingView showProgressHUD:@"加载中"];
    AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
    manager.responseSerializer=[AFHTTPResponseSerializer serializer];
//    NSLog(@"url---%@",path);
    [manager POST:path parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [SXLoadingView hideProgressHUD];
        NSError *error;
        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:&error];
        BOOL succe=NO;
        if ([[dic valueForKey:@"code"]isKindOfClass:[NSNumber class]]) {
            if ([[dic valueForKey:@"code"]intValue]==2) {
                succe=YES;
            }
        }
        if ([[dic valueForKey:@"code"]isKindOfClass:[NSString class]]) {
            if ([[dic valueForKey:@"code"]isEqualToString:@"2"]) {
                succe=YES;
            }
        }
        success(responseObject,succe,dic);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [SXLoadingView hideProgressHUD];
        fail(error);
    }];
}

@end
